package com.example.itshome.fragment;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.viewpager.widget.ViewPager;
import com.example.itshome.R;
import com.example.itshome.adapter.MainPageFragmentAdapter;
import com.example.itshome.adapter.RescuePageRecycleViewAdapter;
import com.example.itshome.entity.RescueStationEntity;
import com.example.itshome.impl.ItemDecorationImpl;
import com.example.itshome.network.AbstractNetworkRequest;
import com.example.itshome.network.AbstractRecycleVIewGetRequest;
import com.example.itshome.network.GetRescueStationRequest;
import com.example.itshome.network.GetSearchRescueStationRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class HelpPageFragment extends Fragment {

    private ViewPager banner;
    private RecyclerView mRecycleView;
    private EditText searchEditText;
    private List<RescueStationEntity> mRecyclerViewData;
    private LinearLayout bannerBottomLayout;
    private int currentBannerIndex;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_help_page, container, false);
        initFind(view);
        initBanner();
        initView();
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        initRecycleView();
    }

    void initFind(View v){
        banner=v.findViewById(R.id.vp_help_page);
        mRecycleView=v.findViewById(R.id.rcv_help_page);
        searchEditText=v.findViewById(R.id.edt_help_page_search);
        bannerBottomLayout=v.findViewById(R.id.layout_help_page_banner_bottom);
    }

    void initView(){
        searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if(actionId== EditorInfo.IME_ACTION_SEARCH){
                    AbstractRecycleVIewGetRequest<RescueStationEntity> request=new GetSearchRescueStationRequest(getActivity())
                            .setSearchValue(textView.getText().toString())
                            .setCurrentData(mRecyclerViewData)
                            .setAdapter(mRecycleView.getAdapter());
                    return true;
                }
                return false;
            }
        });
    }

    void initBanner(){
        List<Fragment> fragments=new ArrayList<>();
        fragments.add(new HelpBannerItem1Fragment());
        fragments.add(new HelpBannerItem2Fragment());
        fragments.add(new HelpBannerItem3Fragment());
        banner.setAdapter(new MainPageFragmentAdapter(getChildFragmentManager(),fragments));
        banner.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                bannerBottomLayout.getChildAt(currentBannerIndex).setEnabled(false);
                bannerBottomLayout.getChildAt(position).setEnabled(true);
                currentBannerIndex=position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        LinearLayout.LayoutParams paramsFirstItem=new LinearLayout.LayoutParams(30,30);
        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(30,30);
        params.setMarginStart(50);
        View circle1=new View(getContext());
        View circle2=new View(getContext());
        View circle3=new View(getContext());
        circle1.setBackgroundResource(R.drawable.slt_help_page_banner_bottom_item);
        circle2.setBackgroundResource(R.drawable.slt_help_page_banner_bottom_item);
        circle3.setBackgroundResource(R.drawable.slt_help_page_banner_bottom_item);
        circle1.setEnabled(false);
        circle2.setEnabled(false);
        circle3.setEnabled(false);
        bannerBottomLayout.addView(circle1,paramsFirstItem);
        bannerBottomLayout.addView(circle2,params);
        bannerBottomLayout.addView(circle3,params);
        bannerBottomLayout.getChildAt(0).setEnabled(true);
        currentBannerIndex=0;
    }

    void initRecycleView(){
        mRecyclerViewData=new ArrayList<>();
        mRecyclerViewData.add(new RescueStationEntity("https://p3-pc.douyinpic.com/img/aweme-avatar/tos-cn-avt-0015_17a998d9c101c125f5d91e6cad39b704~c5_300x300.jpeg?from=2956013662","郝大夫护生园","15602118885"));
        mRecycleView.setAdapter(new RescuePageRecycleViewAdapter(getContext(),mRecyclerViewData));
        mRecycleView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        if(mRecycleView.getItemDecorationCount()==0){
            mRecycleView.addItemDecoration(new ItemDecorationImpl(getContext(),10,10));
        }
        mRecycleView.setNestedScrollingEnabled(false);
        mRecycleView.setFocusable(false);
        AbstractNetworkRequest request=new GetRescueStationRequest(getActivity())
                .setCurrentData(mRecyclerViewData)
                .setAdapter(mRecycleView.getAdapter());
    }
}